package com.hong.service_product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hong.fy_common_commomutil.common.result.Result;
import com.hong.fy_model.model.product.ProductCategoryEntity;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import com.hong.service_product.mapper.ProductCategoryMapper;

import com.hong.service_product.service.ProductCategoryService;

import java.util.List;


@Service("productCategoryService")
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategoryEntity> implements ProductCategoryService {

    //新增商品分类
    @Override
    @CacheEvict(value = "productCategory",allEntries = true)
    public Result addProductCategory(ProductCategoryEntity productCategory) {
        //判断商品分类名是否为空
        if (productCategory.getCategoryName() == null || productCategory.getCategoryName().equals("")) {
            return Result.fail("商品分类名不能为空");
        }

        //判断商品分类名是否存在
        ProductCategoryEntity productCategoryEntity = this.getOne(
                new QueryWrapper<ProductCategoryEntity>().eq("category_name", productCategory.getCategoryName()));
        if(productCategoryEntity != null){
            return Result.fail("商品分类名已存在");
        }else {
            //新增商品分类
            this.save(productCategory);
            return Result.ok("新增商品分类成功");
        }

    }

    //逻辑删除商品分类
    @CacheEvict(value = "productCategory",allEntries = true)
    @Override
    public Boolean logicDeleteProductCategory(Integer categoryId) {
        //判断商品分类id是否存在
        ProductCategoryEntity productCategoryEntity = this.getById(categoryId);
        if(productCategoryEntity == null) {
            return false;
        }else {
            //逻辑删除商品分类
            boolean flag = this.removeById(categoryId);
            return flag;
        }

    }

    //更新商品分类
    @CacheEvict(value = "productCategory",allEntries = true)
    @Override
    public Boolean updateProductCategory(ProductCategoryEntity productCategory) {
        //判断商品分类id是否存在
        ProductCategoryEntity productCategoryEntity = this.getById(productCategory.getCategoryId());
        if(productCategoryEntity == null) {
            return false;
        }else {
            //更新商品分类
            boolean flag = this.updateById(productCategory);
            return flag;
        }
    }

    //分页查询商品分类
    @Cacheable(value = "productCategory",key = "#pageNum + ',' + #limit")
    @Override
    public IPage<ProductCategoryEntity> getProductCategoryListByPage(Integer pageNum, Integer limit) {
        Page page = new Page( pageNum, limit);
        IPage<ProductCategoryEntity> productCategoryEntityIPage = this.page(page);
        return productCategoryEntityIPage;

    }

    //查询所有商品分类
    @Cacheable(value = "productCategory", key = "'allProductCategory'")
    @Override
    public List<ProductCategoryEntity> getProductCategoryList() {
        List<ProductCategoryEntity> productCategoryEntityList = this.list();
        return productCategoryEntityList;
    }

    //根据商品分类id查询商品分类
    @Cacheable(value = "productCategory", key = "#categoryId")
    @Override
    public ProductCategoryEntity getProductCategoryById(Integer categoryId) {

        ProductCategoryEntity productCategoryEntity = this.getById(categoryId);
        return productCategoryEntity;
    }
}